﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Economo.Services.DataContext
{    
    public class AccountTypesDataContext : IDisposable 
    {
        DataClassesDataContext _context = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["Economo"].ConnectionString);

        public AccountType GetAccountTypeById(byte id)
        {
            try
            {
                AccountType accountType = (from b in _context.AccountTypes
                            where b.Id == id                            
                            select b).FirstOrDefault();
                return accountType;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error getting account type by id.", ex));
                return null;
            }
        }

        public Byte InsertAccountType(AccountType newAccountType)
        {
            try
            {

                InsertAccountTypeResult result = _context.InsertAccountType(newAccountType.Description,newAccountType.IsIncome).FirstOrDefault<InsertAccountTypeResult>();
                if (result.Id.HasValue)
                    return result.Id.Value;
                else
                    throw new ExceptionHandling.DataAccessException("Impossible to retrieve autogenerated id when inserting new row.");
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error inserting new account type.", ex));
                return 0;
            }
        }

        public void UpdateAccountType(AccountType accountType)
        {
            try
            {
                AccountType updated = (from b in _context.AccountTypes
                                       where b.Id == accountType.Id
                                select b).First();
                if (updated != null)
                {
                    updated.Description = accountType.Description;
                    updated.IsIncome = accountType.IsIncome;
                    _context.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                }
                else
                    throw new ExceptionHandling.DataAccessException("Account type not found with Id=" + accountType.Id.ToString());
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error updating account type.", ex));
            }
        }
        public void DeleteAccountType(AccountType accountType)
        {
            try
            {
                _context.AccountTypes.DeleteOnSubmit(accountType);
                _context.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error deleting account type.", ex));
            }
        }
        public void DeleteAccountType(byte id)
        {
            try
            {
                AccountType accountType = GetAccountTypeById(id);
                if (accountType != null)
                    DeleteAccountType(accountType);
                else
                    throw new ExceptionHandling.DataAccessException("Account type to delete not found.");
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error deleting account type.", ex));
            }
        }
        public void Dispose()
        {
            _context.Dispose();
        }
    }
}
